<?php


namespace app\services\system\config;


use app\dao\system\config\SystemConfigTabDao;
use app\services\BaseService;

/**
 * 系统配置分类
 * Class SystemConfigTabServices
 * @package app\services\system\config
 */
class SystemConfigTabService extends BaseService
{
    /**
     * SystemConfigTabServices constructor.
     *
     * @param SystemConfigTabDao $dao
     */
    public function __construct(SystemConfigTabDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * Description: 系统设置头部分类读取
     *
     * @param int $pid
     *
     * @return array|\basic\BaseModel[]|\think\Collection|\think\Model[]
     * @throws \ReflectionException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getConfigTab(int $pid, int $isChildren = 1, int $status = 1)
    {
        $map = ['pid' => $pid];
        if ($status !== -1) {
            $map['status'] = $status;
        }
        $list = $this->dao->getConfigTabAll($map, ['id', 'id as value', 'title as label', 'pid', 'icon', 'type', 'status'],
            $pid ? [] : [['type', '=', '0']])->toArray();
        return $isChildren ? get_tree_children($list) : $list;
    }

    /**
     * 获取配置分类列表
     *
     * @param array $where
     *
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getConfgTabList(array $where)
    {
        [$page, $limit] = $this->getPageValue();
        $list       = $this->dao->getConfgTabList($where, $page, $limit);
        $count      = $this->dao->count($where);
        $menusValue = [];
        foreach ($list as $item) {
            $menusValue[] = $item->getData();
        }
        $list = get_tree_children($menusValue);
        return compact('list', 'count');
    }

    /**
     * 获取配置分类选择下拉树
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSelectForm()
    {
        $menuList = $this->dao->getConfigTabAll([], ['id', 'pid', 'title'])->toArray();
        $list     = sort_list_tier($menuList, 0, 'pid', 'id');
        $menus    = [['value' => 0, 'label' => '顶级按钮']];
        foreach ($list as $menu) {
            $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['title']];
        }
        return $menus;
    }

}
